/*
ehs - 2010-05-14
Flickr photoset

2010-06-05
----------
- upped per-page to 50
- added a bunch of todo's with the eventual goals of clearer code path and custom pagination
*/


function FlickrPhotoset(setId_, proxyUrl_, authToken_, updatedCallback_, doneCallback_)
{
	// fixme: should this use "public object" paradigm?
	var that_ = this; // required for private function support

	this.photos = new Array();

	var photoCount = 0;
	$.get(proxyUrl_,
		{
			method: "flickr.photosets.getInfo",
			auth_token: authToken_,
			photoset_id: setId_
		},
		function(data, textStatus) {
			that_.onGetInfoCompleted_(data, textStatus);
		},
		"json");


	this.onGetInfoCompleted_ = function(data, textStatus)
	{
		if (textStatus != "success" || data.stat != "ok")
			return;

		photoCount = data.photoset.photos;
		updatedCallback_(); // not much has happened yet, but that's OK
		retrievePhotos_();
	}


	function retrievePhotos_()
	{
		if (that_.photos.length >= photoCount) {
			if (doneCallback_ != null)
				doneCallback_();
			return;
		}

		var lastPage = that_.photos.length / 50;
		var pageToRetrieve = lastPage + 1;

		$.get(proxyUrl_,
			{
				method: "flickr.photosets.getPhotos",
				auth_token: authToken_,
				photoset_id: setId_,
				per_page: 50,
				page: pageToRetrieve,
				extras: "date_taken,date_upload,o_dims" // fixme: can we get original photo name?
			},
			function(data, textStatus) {
				that_.onGetPhotosCompleted_(data, textStatus);
			},
			"json");
	}


	this.onGetPhotosCompleted_ = function(data, textStatus)
	{
		if (textStatus != "success" || data.stat != "ok")
			return;

		if (data.photoset.photo.length > 0) {
			this.photos = this.photos.concat(data.photoset.photo);
			updatedCallback_();
			retrievePhotos_();
		}
		else if (doneCallback != null)
			doneCallback();
	}

	// todo: only retrieve info when created
	// todo: return public object
	// todo: make client explicitly request photos with separate callback (from public object)
	// todo: wait on photo retrieval until info has been received
	// todo: *** allow custom pagination through new retrievePhotos method ***
}

